﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Cn.Edwon.Web.Support;
using System.Text;
using Cn.Cellula.Model;
using Cn.Edwon.Biz;
using Cn.Edwon.Spring;
using System.Collections.Generic;
using Cn.Cellula.Model.Content;
using Cn.Edwon.Web.Script;

namespace Cn.Cellula.Website.Ws {
    /// <summary>
    /// 单体EO的list，配合数据表或视图，返回一个单体EO的list集合，并转换成Json格式
    /// 根据不同的参数，进行不同的查询
    /// </summary>
    public partial class _SList : PageSupport {
        protected void Page_Load(object sender, EventArgs e) {
            CommonParms();
            if (Request.QueryString["act"] == "product") {
                ProductList();
            } if(Request.QueryString["act"] == "guestbook"){
                GuestbookList();
            } else {
                VoidAction(); 
            }

        }

        #region 私有方法
        private void VoidAction() {
            ResponseOut("null");
        }
        /// <summary>
        /// 输出内容
        /// </summary>
        /// <param name="rContent"></param>
        private void ResponseOut(String rContent) {
            Response.HeaderEncoding = Encoding.UTF8;
            Response.Clear();
            Response.Write(rContent);
            Response.End();
        }
        #endregion

        #region 参数集定义
        //显示每页的数据量，相当于显示条数
        Int32 _PageSize = 0;
        //页码索引
        Int32 _PageIndex = 1;
        //排序字段
        String _OrderBy = "";
        //字段过滤
        String _Filter = "";
        /// <summary>
        /// 全局参数初始化
        /// </summary>
        private void CommonParms() {
            //参数集:
            //显示每页的数据量，相当于显示条数
            try {
                _PageSize = Convert.ToInt32(Request.QueryString["ps"]);
            } catch {
            }
            if (_PageSize < 1) _PageSize = 0;
            //页码索引
            try {
                _PageIndex = Convert.ToInt32(Request.QueryString["pi"]);
            } catch {
            }
            if (_PageIndex < 0) {
                _PageIndex = 1;
            }
            //排序字段
            _OrderBy = Request.QueryString["ob"];
            //字段过滤
            _Filter = Request.QueryString["f"];
        }
        #endregion

        #region 产品列表
        private void ProductList() {
            String hql = " from VProductListFullEO eo ";
            String mode = Request.QueryString["mode"];

            if (Request.QueryString["market"] != null) {
                hql += "eo.MarketKey='"+Request.QueryString["market"]+"' ";
            }
            
            if (mode == "new") {
                //按最后更新时间的逆序排列
                hql += " order by eo.LastDate desc ";
            } else {
                hql += " order by eo.LastDate desc ";
            }

            if (_PageSize > 0) {
                hql += " limit 0," + _PageSize;
            }

            IBiz biz = BizFactory.Get<IBiz>();
            IList<Object> list = biz.Find<Object>(hql);
            String json = JsUtil.ParseEOList2Json(list);
            ResponseOut(json);
            
        }
        #endregion

        #region 留言板
        private void GuestbookList() {
            IList<TGuestbookEO> list = BizFactory.Get<IBiz>().LoadAll<TGuestbookEO>();
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            for (int i = 0; i < list.Count;i++ ) {
                TGuestbookEO eo = list[i];
                if (i > 0) {
                    sb.Append(",");
                }
                sb.Append(JsUtil.ParseEO2Json(eo));
            }
            sb.Append("]");
            ResponseOut(sb.ToString());
        }
        #endregion
    }
}
